package com.nice.demo.test;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.view.ViewGroup.LayoutParams;
import androidx.annotation.NonNull;

import com.nice.demo.MyHandCache;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class HandWritingViewHelper {
    private static String TAG = "HandWritingViewHelper";
    public static boolean DEBUG = true;
    public static final String HEIGHT = "height";
    public static final String WIDTH = "width";
    public static final String STROKE_HEIGHT = "stroke_height";
    public static final String STROKE_WIDTH = "stroke_width";
    static int sDefMinHeight = 275;
    private static String[] allSpanDevice = new String[]{"GT-N5100", "GT-N5110", "GT-N5120", "GT-N8000", "GT-N8010", "SM-P350", "SM-P355C", "SM-P600"};
    private static String[] allSpecialDevices = new String[]{"SP1099V"};

    private HandWritingViewHelper() {
    }

    public static MyHandWritingView getHandWriteViewByStrokeOrDefault(Context context, String stroke, int width, int height) {
        MyHandWritingView handWritingView;
        if (stroke != null && stroke.contains("@")) {
            handWritingView = getHandWriteViewByStroke(context, stroke);
        } else {
            handWritingView = new MyHandWritingView(context, width, height);
            handWritingView.setToWriting();
        }

        return handWritingView;
    }

    public static MyHandWritingView getHandWriteViewByStroke(Context context, String stroke) {
        Map<String, Integer> widthHeight = getWidthAndHeight(stroke);
        if (widthHeight == null) {
            return null;
        } else {
            MyHandWritingView handWritingView = new MyHandWritingView(context, (Integer)widthHeight.get("width"), (Integer)widthHeight.get("height"));
          //  handWritingView.restoreToImage(stroke);
            handWritingView.setToWriting();
            return handWritingView;
        }
    }

    public static MyHandWritingView getHandWriteViewByStroke(Context context, String stroke, boolean isCutStrokeHeight) {
        return getHandWriteViewByStroke(context, stroke, sDefMinHeight, isCutStrokeHeight);
    }

    public static MyHandWritingView getHandWriteViewByStroke(Context context, String stroke, int minHeight, boolean isCutStrokeHeight) {
        Map<String, Integer> widthHeight = getWidthAndHeight(stroke);
        if (widthHeight == null) {
            return null;
        } else {
            int height;
            if (isCutStrokeHeight) {
                if ((Integer)widthHeight.get("stroke_height") == 0) {
                    height = (Integer)widthHeight.get("height");
                } else {
                    height = Math.min((Integer)widthHeight.get("stroke_height") + 2, (Integer)widthHeight.get("height"));
                    height = Math.max(height, minHeight);
                }
            } else {
                height = (Integer)widthHeight.get("height");
            }

            MyHandWritingView handWritingView = new MyHandWritingView(context, (Integer)widthHeight.get("width"), height);
        //    handWritingView.restoreToImage(stroke);
            handWritingView.setToWriting();
            return handWritingView;
        }
    }

    public static void getHandWriteViewByStroke(@NonNull MyHandWritingView handWritingView, String stroke, boolean isCutStrokeHeight) {
        getHandWriteViewByStroke(handWritingView, stroke, sDefMinHeight, isCutStrokeHeight);
    }

    public static void getHandWriteViewByStroke(@NonNull MyHandWritingView handWritingView, String stroke, int minHeight, boolean isCutStrokeHeight) {
        Map<String, Integer> widthHeight = getWidthAndHeight(stroke);
        if (widthHeight != null) {
            int height;
            if (isCutStrokeHeight) {
                if ((Integer)widthHeight.get("stroke_height") == 0) {
                    height = (Integer)widthHeight.get("height");
                } else {
                    height = Math.min((Integer)widthHeight.get("stroke_height") + 2, (Integer)widthHeight.get("height"));
                    height = Math.max(height, minHeight);
                }
            } else {
                height = (Integer)widthHeight.get("height");
            }

            LayoutParams layoutParams = handWritingView.getLayoutParams();
            layoutParams.height = height;
            handWritingView.setLayoutParams(layoutParams);
          //  handWritingView.restoreToImage(stroke);
            handWritingView.setToWriting();
        }
    }

    public static String mergeStrokes(String[] strokes) {
        int height = 0;
        int width = 0;
        int strokeHeight = 0;
        int strokeWidth = 0;
        String mergePaths = null;
        if (strokes == null) {
            return null;
        } else {
            String[] var6 = strokes;
            int var7 = strokes.length;

            for(int var8 = 0; var8 < var7; ++var8) {
                String stroke = var6[var8];
                Map<String, Integer> widthHeight = getWidthAndHeight(stroke);
                if (widthHeight != null) {
                    height = Math.max(height, (Integer)widthHeight.get("height"));
                    width = Math.max(width, (Integer)widthHeight.get("width"));
                    strokeHeight = Math.max(strokeHeight, (Integer)widthHeight.get("stroke_height"));
                    strokeWidth = Math.max(strokeWidth, (Integer)widthHeight.get("stroke_width"));
                    String paths = getPaths(stroke);
                    if (paths != null) {
                        if (mergePaths == null) {
                            mergePaths = paths;
                        } else {
                            mergePaths = mergePaths + "=" + paths;
                        }
                    }
                }
            }

            if (mergePaths == null) {
                return null;
            } else {
                return width + "," + height + "," + strokeWidth + "," + strokeHeight + "&" + "1.0" + "@" + mergePaths;
            }
        }
    }

    public static int[] getImageSize(String s) {
        int[] ss = new int[2];

        try {
            String mStrImage = new String(BASE64.decode(s));
            String[] mImageArr = mStrImage.split("&");
            if (mImageArr.length > 0) {
                String[] strArr = mImageArr[0].split(",");
                int width = toInt(strArr[0]);
                int height = toInt(strArr[1]);
                ss[0] = width;
                ss[1] = height;
            }
        } catch (UnsupportedEncodingException var7) {
            var7.printStackTrace();
        }

        return ss;
    }

    public static Map<String, Integer> getWidthAndHeight(String drawPath) {
        if (TextUtils.isEmpty(drawPath)) {
            return null;
        } else {
            String str = drawPath.split("&")[0];
            String[] size = str.split(",");
            int height = 0;
            int width = 0;
            int strokeHeight = 0;
            int strokeWidth = 0;
            if (size.length == 2 || size.length == 4) {
                width = toInt(size[0]);
                height = toInt(size[1]);
                if (size.length == 4) {
                    strokeWidth = toInt(size[2]);
                    strokeHeight = toInt(size[3]);
                }
            }

            Map<String, Integer> map = new HashMap();
            map.put("height", height);
            map.put("width", width);
            map.put("stroke_width", strokeWidth);
            map.put("stroke_height", strokeHeight);
            return map;
        }
    }

    public static String getPaths(String drawPath) {
        if (TextUtils.isEmpty(drawPath)) {
            return null;
        } else {
            String[] str = drawPath.split("@");
            return str.length == 2 ? str[1] : null;
        }
    }

    static int toInt(String text) {
        return toInt(text, 0);
    }

    static int toInt(String text, int value) {
        if (TextUtils.isEmpty(text)) {
            return value;
        } else {
            int result = value;

            try {
                if (text.contains(".")) {
                    result = (int) MyHandCache.parseFloat(text);
                } else {
                    result = MyHandCache.parseInt(text);
                }
            } catch (Exception var4) {
                if (DEBUG) {
                    XHLog.i(TAG, "toInt fail, text=" + text, new Object[0]);
                }

                var4.printStackTrace();
            }

            return result;
        }
    }


    static long toLong(String text, Long value) {
        if (TextUtils.isEmpty(text)) {
            return value;
        } else {
            long result = value;
            try {
                if (text.contains(".")) {
                    result = (Long) MyHandCache.parseLong(text);
                } else {
                    result = MyHandCache.parseLong(text);
                }
            } catch (Exception var4) {
                if (DEBUG) {
                    XHLog.i(TAG, "toInt fail, text=" + text, new Object[0]);
                }

                var4.printStackTrace();
            }

            return result;
        }
    }

    public static boolean isSpanDevice() {
        String device_model = Build.MODEL;
        String[] var1 = allSpanDevice;
        int var2 = var1.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            String dname = var1[var3];
            if (dname.equals(device_model)) {
                return true;
            }
        }

        return false;
    }

    public static boolean isSpecialDevice() {
        return true;
    }
}
